package cn.kicsy.www.anonymous_bbs.controller;

import cn.kicsy.www.anonymous_bbs.service.BanService;
import cn.kicsy.www.anonymous_bbs.service.BanStatusResponse;
import cn.kicsy.www.anonymous_bbs.service.SensitiveWordFilterService;
import cn.kicsy.www.anonymous_bbs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "管理员功能", description = "管理员专用的API接口")
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    
    @Autowired
    private SensitiveWordFilterService sensitiveWordFilterService;
    
    @Autowired
    private BanService banService;
    
    @Autowired
    private AdminService adminService;
    
    /**
     * 添加敏感词
     * @param request 敏感词请求
     * @return 操作结果
     */
    @Operation(summary = "添加敏感词", description = "添加新的敏感词到过滤系统")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "添加成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class))),
        @ApiResponse(responseCode = "400", description = "添加失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
        @ApiResponse(responseCode = "401", description = "管理员密码验证失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
    })
    @PostMapping("/sensitive-words")
    public ResponseEntity<?> addSensitiveWord(@RequestBody @Parameter(description = "敏感词请求") SensitiveWordRequest request) {
        // 验证管理员密码
        if (!adminService.authenticate(request.getPassword())) {
            return ResponseEntity.status(401).body(new ErrorResponse("管理员密码验证失败"));
        }
        
        if (request.getWord() == null || request.getWord().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(new ErrorResponse("敏感词不能为空"));
        }
        
        sensitiveWordFilterService.addSensitiveWord(request.getWord());
        return ResponseEntity.ok(new SuccessResponse("敏感词添加成功"));
    }
    
    /**
     * 移除敏感词
     * @param request 敏感词请求
     * @return 操作结果
     */
    @Operation(summary = "移除敏感词", description = "从过滤系统中移除敏感词")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "移除成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class))),
        @ApiResponse(responseCode = "400", description = "移除失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
        @ApiResponse(responseCode = "401", description = "管理员密码验证失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
    })
    @DeleteMapping("/sensitive-words")
    public ResponseEntity<?> removeSensitiveWord(@RequestBody @Parameter(description = "敏感词请求") SensitiveWordRequest request) {
        // 验证管理员密码
        if (!adminService.authenticate(request.getPassword())) {
            return ResponseEntity.status(401).body(new ErrorResponse("管理员密码验证失败"));
        }
        
        if (request.getWord() == null || request.getWord().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(new ErrorResponse("敏感词不能为空"));
        }
        
        sensitiveWordFilterService.removeSensitiveWord(request.getWord());
        return ResponseEntity.ok(new SuccessResponse("敏感词移除成功"));
    }
    
    /**
     * 永久封禁IP地址
     * @param request 封禁请求
     * @return 操作结果
     */
    @Operation(summary = "永久封禁IP", description = "永久封禁指定的IP地址")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "封禁成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class))),
        @ApiResponse(responseCode = "400", description = "封禁失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
        @ApiResponse(responseCode = "401", description = "管理员密码验证失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
    })
    @PostMapping("/ban/permanent")
    public ResponseEntity<?> banPermanently(@RequestBody @Parameter(description = "封禁请求") BanRequest request) {
        // 验证管理员密码
        if (!adminService.authenticate(request.getPassword())) {
            return ResponseEntity.status(401).body(new ErrorResponse("管理员密码验证失败"));
        }
        
        if (request.getIpAddress() == null || request.getIpAddress().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(new ErrorResponse("IP地址不能为空"));
        }
        
        if (request.getReason() == null || request.getReason().trim().isEmpty()) {
            request.setReason("违反社区规定");
        }
        
        // 使用IP地址作为唯一标识进行封禁
        banService.banPermanently(request.getIpAddress(), request.getReason());
        return ResponseEntity.ok(new SuccessResponse("IP地址已永久封禁"));
    }
    
    /**
     * 临时封禁IP地址
     * @param request 临时封禁请求
     * @return 操作结果
     */
    @Operation(summary = "临时封禁IP", description = "临时封禁指定的IP地址一段时间")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "封禁成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class))),
        @ApiResponse(responseCode = "400", description = "封禁失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
        @ApiResponse(responseCode = "401", description = "管理员密码验证失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
    })
    @PostMapping("/ban/temporary")
    public ResponseEntity<?> banTemporarily(@RequestBody @Parameter(description = "临时封禁请求") TemporaryBanRequest request) {
        // 验证管理员密码
        if (!adminService.authenticate(request.getPassword())) {
            return ResponseEntity.status(401).body(new ErrorResponse("管理员密码验证失败"));
        }
        
        if (request.getIpAddress() == null || request.getIpAddress().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(new ErrorResponse("IP地址不能为空"));
        }
        
        if (request.getHours() == null || request.getHours() <= 0) {
            return ResponseEntity.badRequest().body(new ErrorResponse("封禁时长必须大于0"));
        }
        
        if (request.getReason() == null || request.getReason().trim().isEmpty()) {
            request.setReason("违反社区规定");
        }
        
        // 使用IP地址作为唯一标识进行封禁
        banService.banTemporarily(request.getIpAddress(), request.getReason(), request.getHours());
        return ResponseEntity.ok(new SuccessResponse("IP地址已临时封禁"));
    }
    
    /**
     * 解除对IP地址的封禁
     * @param request 解除封禁请求
     * @return 操作结果
     */
    @Operation(summary = "解除IP封禁", description = "解除对指定IP地址的封禁")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "解除成功", content = @Content(schema = @Schema(implementation = SuccessResponse.class))),
        @ApiResponse(responseCode = "400", description = "解除失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
        @ApiResponse(responseCode = "401", description = "管理员密码验证失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
    })
    @DeleteMapping("/ban")
    public ResponseEntity<?> unban(@RequestBody @Parameter(description = "解除封禁请求") UnbanRequest request) {
        // 验证管理员密码
        if (!adminService.authenticate(request.getPassword())) {
            return ResponseEntity.status(401).body(new ErrorResponse("管理员密码验证失败"));
        }
        
        if (request.getIpAddress() == null || request.getIpAddress().trim().isEmpty()) {
            return ResponseEntity.badRequest().body(new ErrorResponse("IP地址不能为空"));
        }
        
        // 使用IP地址作为唯一标识进行解封
        banService.unban(request.getIpAddress());
        return ResponseEntity.ok(new SuccessResponse("IP地址已解除封禁"));
    }
    
    /**
     * 检查IP地址是否被封禁
     * @param ipAddress IP地址
     * @return 封禁状态
     */
    @Operation(summary = "检查IP封禁状态", description = "检查指定IP地址是否被封禁")
    @ApiResponses({
        @ApiResponse(responseCode = "200", description = "检查成功", content = @Content(schema = @Schema(implementation = BanStatusResponse.class))),
        @ApiResponse(responseCode = "400", description = "检查失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
        @ApiResponse(responseCode = "401", description = "管理员密码验证失败", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
    })
    @GetMapping("/ban/status")
    public ResponseEntity<?> checkBanStatus(@Parameter(description = "IP地址") @RequestParam String ipAddress,
            @Parameter(description = "管理员密码") @RequestParam String password) {
        // 验证管理员密码
        if (!adminService.authenticate(password)) {
            return ResponseEntity.status(401).body(new ErrorResponse("管理员密码验证失败"));
        }
        
        if (ipAddress == null || ipAddress.trim().isEmpty()) {
            return ResponseEntity.badRequest().body(new ErrorResponse("IP地址不能为空"));
        }
        
        // 使用IP地址作为唯一标识检查封禁状态
        BanStatusResponse response = banService.checkBanStatus(ipAddress);
        return ResponseEntity.ok(response);
    }
    
    // 请求和响应模型类
    @Schema(description = "管理员验证请求")
    public static class AdminAuthRequest {
        private String password;
        
        public String getPassword() {
            return password;
        }
        
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    @Schema(description = "敏感词请求")
    public static class SensitiveWordRequest extends AdminAuthRequest {
        private String word;
        
        public String getWord() {
            return word;
        }
        
        public void setWord(String word) {
            this.word = word;
        }
    }
    
    @Schema(description = "封禁请求")
    public static class BanRequest extends AdminAuthRequest {
        private String ipAddress;
        private String reason;
        
        public String getIpAddress() {
            return ipAddress;
        }
        
        public void setIpAddress(String ipAddress) {
            this.ipAddress = ipAddress;
        }
        
        public String getReason() {
            return reason;
        }
        
        public void setReason(String reason) {
            this.reason = reason;
        }
    }
    
    @Schema(description = "临时封禁请求")
    public static class TemporaryBanRequest extends BanRequest {
        private Integer hours;
        
        public Integer getHours() {
            return hours;
        }
        
        public void setHours(Integer hours) {
            this.hours = hours;
        }
    }
    
    @Schema(description = "解除封禁请求")
    public static class UnbanRequest extends AdminAuthRequest {
        private String ipAddress;
        
        public String getIpAddress() {
            return ipAddress;
        }
        
        public void setIpAddress(String ipAddress) {
            this.ipAddress = ipAddress;
        }
    }
    
    @Schema(description = "成功响应")
    public static class SuccessResponse {
        private String message;
        
        public SuccessResponse(String message) {
            this.message = message;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
    @Schema(description = "错误响应")
    public static class ErrorResponse {
        private String message;
        
        public ErrorResponse(String message) {
            this.message = message;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
}